﻿//===============================OCRFORM.cs==============================\\
//===Bart Steijlen======\\


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Team2_Vision
{
    public partial class OCRForm : Form
    {
        PictureBox[] pictureboxarray = new PictureBox[100];
        PictureBox[] pictureboxarraycopy = new PictureBox[100];
        static int parameter = 0;
        static String stringoutput;
        public static Boolean ready = true;
        public OCRForm()
        {
            InitializeComponent();
            
        }

        public static void setParameter(int waarde)
        {
            parameter = waarde;
        }

        private void projectsToolStripMenuItem_Click(object sender, EventArgs e)
        {

            MainForm main = new MainForm();
            main.Show();
            this.Visible = false;
        }

        private void GenerateRegistrationPlateButton_Click(object sender, EventArgs e)
        {
            GenerateRegistrationPlate();
                        
        }  
 

        private void impulseNoiseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VisionAlgorithm Noise = new ImpulseNoise("impulse noise");
            ParameterForm para = new ParameterForm(2);
            para.ParameterText.Text = "Give Noise Percentage:";
            para.ParameterSliderMatrix.Visible = false;
            para.ParameterSliderPercentage.Visible = true;
            para.ParameterTextBox.Text = 1 + "%";
            para.ShowDialog();
            if (parameter != 0)
            {
                backgroundWorker1.RunWorkerAsync(Noise);
            }            
        }

        private void bitFlipNoiseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VisionAlgorithm Noise = new BitFlipNoise("Bit Flip noise");
            ParameterForm para = new ParameterForm(2);
            para.ParameterText.Text = "Give Noise Percentage:";
            para.ParameterSliderMatrix.Visible = false;
            para.ParameterSliderPercentage.Visible = true;
            para.ParameterTextBox.Text = 1 + "%";
            para.ShowDialog();
            if (parameter != 0)
            {
                backgroundWorker1.RunWorkerAsync(Noise);
            }            
        }

        private void randomNoiseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VisionAlgorithm Noise = new RandomNoise("Random noise");
            ParameterForm para = new ParameterForm(2);
            para.ParameterText.Text = "Give Noise Percentage:";
            para.ParameterSliderMatrix.Visible = false;
            para.ParameterSliderPercentage.Visible = true;
            para.ParameterTextBox.Text = 1 + "%";
            para.ShowDialog();
            if (parameter != 0)
            {
                backgroundWorker1.RunWorkerAsync(Noise);
            }            
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            ready = false;
            VisionAlgorithm vision = (VisionAlgorithm)e.Argument;
            foreach(PictureBox pic in pictureboxarray) {
                if(pic != null) {
                    Bitmap newBitmap = (Bitmap)pic.Image;
                    pic.Image = vision.DoAlgorithm(newBitmap, parameter);
                }
                else {
                    break;
                }                
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OCRGeneratorInput.Text = "";
            char[] charsLetters = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
            char[] charsCijfers = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
            Random rand = new Random();

            int cijferPositie = rand.Next(0, 3);

            for (int i = 0; i < 2; i++)
            {
                if (cijferPositie == 0)
                {
                    OCRGeneratorInput.Text += charsCijfers[rand.Next(9)];
                }
                else
                {
                    OCRGeneratorInput.Text += charsLetters[rand.Next(25)];
                }
            }
            OCRGeneratorInput.Text += "-";
            for (int i = 0; i < 2; i++)
            {
                if (cijferPositie == 1)
                {
                    OCRGeneratorInput.Text += charsCijfers[rand.Next(9)];
                }
                else
                {
                    OCRGeneratorInput.Text += charsLetters[rand.Next(25)];
                }
            }
            OCRGeneratorInput.Text += "-";
            for (int i = 0; i < 2; i++)
            {
                if (cijferPositie == 2)
                {
                    OCRGeneratorInput.Text += charsCijfers[rand.Next(9)];
                }
                else
                {
                    OCRGeneratorInput.Text += charsLetters[rand.Next(25)];
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            stringoutput = "";

            VisionAlgorithm OCR = new Resize("OCR");
            backgroundWorker1.RunWorkerAsync(OCR);
            OCRoutput.Text = stringoutput;
            Point punt = new Point((groupBox2.Width - OCRoutput.Width) / 2, 38);
            OCRoutput.Location = punt;
        }

        private void OCRGeneratorInput_MouseClick(object sender, MouseEventArgs e)
        {
            OCRGeneratorInput.BackColor = Color.White;
            OCRGeneratorInput.Text = "";
        }

        private void ruinPixelsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VisionAlgorithm Noise = new RuinPixels("Random noise");
            ParameterForm para = new ParameterForm(2);
            para.ParameterText.Text = "Give Ruin Percentage:";
            para.ParameterSliderMatrix.Visible = false;
            para.ParameterSliderPercentage.Visible = true;
            para.ParameterTextBox.Text = 1 + "%";
            para.ShowDialog();
            if (parameter != 0)
            {
                backgroundWorker1.RunWorkerAsync(Noise);
            }            
        }

        private void ruinLinesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VisionAlgorithm Noise = new RuinLines("Random noise");
            ParameterForm para = new ParameterForm(2);
            para.ParameterText.Text = "Give Ruin Percentage:";
            para.ParameterSliderMatrix.Visible = false;
            para.ParameterSliderPercentage.Visible = true;
            para.ParameterTextBox.Text = 1 + "%";
            para.ShowDialog();
            if (parameter != 0)
            {
                backgroundWorker1.RunWorkerAsync(Noise);
            }
        }

        private void OCRForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            MainForm main = new MainForm();
            main.Show();
            this.Visible = false;
        }

        public static void appendOutput(char u)
        {
            stringoutput += u;
            //Console.WriteLine("Output is nu: " + stringoutput);
        }

        private void ruinClustersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VisionAlgorithm Noise = new RuinCluster("Random noise");
            ParameterForm para = new ParameterForm(2);
            para.ParameterText.Text = "Give Ruin Percentage:";
            para.ParameterSliderMatrix.Visible = false;
            para.ParameterSliderPercentage.Visible = true;
            para.ParameterTextBox.Text = 1 + "%";
            para.ShowDialog();
            if (parameter != 0)
            {
                backgroundWorker1.RunWorkerAsync(Noise);
            }
        }

        private void OCRtestButton_Click(object sender, EventArgs e)
        {
            
            VisionAlgorithm vision = new RuinCluster("");
            
            for (int i = 0; i <= 100; i+=4)
            {
                GenerateRegistrationPlate();
                parameter = i;
                foreach (PictureBox a in pictureboxarray)
                {

                    if (a == null)
                    {
                        break;
                    }
                    Bitmap newBitmap = new Bitmap(a.Image);
                  //  Console.WriteLine(parameter);
                    a.Image = vision.DoAlgorithm(newBitmap, parameter);


                }

                pictureboxarraycopy = pictureboxarray;
                runOCR();


                if (OCRoutput.Text != OCRGeneratorInput.Text)
                {

                    break;
                }

            }
               
        }

        
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            OCRoutput.Text = stringoutput;
        }

        private void GenerateRegistrationPlate()
        {
            panel1.Controls.Clear();
            for (int j = 0; j < pictureboxarray.Length; j++)
            {
                pictureboxarray[j] = null;
            }
            Padding pad = new Padding(0, 0, 0, 0);
            Bitmap start = new Bitmap(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + "\\Kentekens\\logo.jpg");
            PictureBox startpic = new PictureBox();
            startpic.Image = start;
            startpic.Width = start.Width;
            startpic.Height = start.Height;
            startpic.Margin = pad;
            panel1.Controls.Add(startpic);
            String generate = OCRGeneratorInput.Text;
            String character;
            int i = 0;
            try
            {
                for (i = 0; i < generate.Length; i++)
                {
                    Char charac = generate.ElementAt(i);
                    if (charac == ' ')
                    {
                        charac = '-';
                    }
                    if (char.IsLower(charac))
                    {
                        charac = char.ToUpper(charac);
                    }
                    character = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + "\\Kentekens\\" + charac + ".jpg";

                    Bitmap letter = new Bitmap(character);
                    PictureBox pic = new PictureBox();

                    pic.Image = letter;
                    pic.Height = letter.Height;
                    pic.Width = letter.Width;
                    pic.Name = "picbox" + i;

                    pic.Margin = pad;
                    panel1.Controls.Add(pic);
                    pictureboxarray[i] = pic;

                }

            }
            catch (Exception)
            {
                OCRGeneratorInput.Text = "ILLEGAL CHARACTER";
                OCRGeneratorInput.BackColor = Color.Red;
                panel1.Controls.Clear();
            }
        }

        private void runOCR()
        {

            stringoutput = "";

            VisionAlgorithm OCR = new Resize("OCR");
            
            foreach (PictureBox pic in pictureboxarray)
            {
                if (pic != null)
                {
                    Bitmap newBitmap = (Bitmap)pic.Image;
                    pic.Image = OCR.DoAlgorithm(newBitmap, parameter);
                }
                else
                {
                    break;
                }
            }

      
            OCRoutput.Text = stringoutput;
            Point punt = new Point((groupBox2.Width - OCRoutput.Width) / 2, 38);
            OCRoutput.Location = punt;

        }

 


    }
}
